/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contoller;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import model.dao.util.BaoChiVietDAO;
import model.dao.util.SuKienTinTucDAO;
import model.pojo.BaoChiViet;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Phi
 */
@WebServlet(name = "ThemBaoChiViet.do", urlPatterns = {"/ThemBaoChiViet.do"})
public class ThemBaoChiViet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("Action");
        int kqthem;
        int matt = BaoChiVietDAO.LayMa() + 1;
        if(action !=null){
            if (action.equals("View")) {             
                String view = "thembaochiviet.jsp";
                RequestDispatcher rd = request.getRequestDispatcher(view);
                rd.forward(request, response);
            }
            if(action.equals("Nap")) {   
            boolean ktraMultiPart = ServletFileUpload.isMultipartContent(request);
            if (!ktraMultiPart) {
                //todo
                return;
            } else {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = null;
                try {
                    items = upload.parseRequest(request);
                } catch (FileUploadException ex) {
                    ex.printStackTrace();
                }
                Iterator iter = items.iterator();
                Hashtable params = new Hashtable();
                String fileName = null;
                String duongdan = null;
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (item.isFormField()) {
                        params.put(item.getFieldName(), item.getString("UTF-8"));
                    } else {
                        try {
                            duongdan = getServletContext().getRealPath("/") + "images\\" + matt + ".jpg";
                            File saveFile = new File(duongdan);
                            item.write(saveFile);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            String tieuDe=(String) params.get("txtTieuDe");
            String noiDung=(String) params.get("txtNoiDung");
            String ndChiTiet=(String) params.get("trNoiDungChiTiet");
            Date todayD = new Date(System.currentTimeMillis());
            BaoChiViet sktt=new BaoChiViet();
            sktt.setLogo(matt+".jpg");
            sktt.setTitle(tieuDe);
            sktt.setNoiDung(noiDung);
            sktt.setNoiDungMore(ndChiTiet);   
            sktt.setNgayViet(todayD);
            boolean themtt=BaoChiVietDAO.ThemTinTuc(sktt);
            //int kq;
            String loi="";
            String view="thembaochiviet.jsp";
            if(themtt==true){
                loi="Thêm thành công";
                
            }
            else{
                loi="Thêm thất bại";
            }
            request.setAttribute("them", loi);
            RequestDispatcher rd = request.getRequestDispatcher(view);
            rd.forward(request, response);
        }
        }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
